<?php 
    //引入类
    require_once("file_exceptions.php");

    //create short variable names - 创建短变量名
    $tireqty = (int) $_POST['tireqty'];//（整型变量）轮胎
    $oilqty = (int) $_POST['oilqty'];//油
    $sparkqty = (int) $_POST['sparkqty'];//火花塞
    $address = preg_replace('/\t|\R/', ' ', $_POST['address']);//收件地址
    $document_root = $_SERVER['DOCUMENT_ROOT'];
    $date = date(' Y-m-j H:i');//时间格式
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Bob`s Auto Parts - Order Results</title>

</head>
<body>
    

<h1>Bob`s Auto Parts - 鲍勃的汽车配件</h1>
<h2>Order Results - 订单结果</h2>

<?php 
    echo "<p>Order processed at-订单处理时间".date(' Y-m-j H:i').'</p>';

    
    
    

    
    //打印变量
    //echo $tireqty.'tires - 轮胎<br />';
    //echo $address.'收货地址<br />';
    //echo htmlspecialchars($tireqty).'tires - 轮胎<br />';//把预定义的字符 "<" （小于）和 ">" （大于）转换为 HTML 实体：
    
    echo '<p>Your order is as follows-您的订单如下: </p>';//您的订单如下：
    $totalqty = 0;
    $totalamount=0.00;//浮点变量

     //出售商品的价格 - 常量
     define('TIREPRICE', 100);//轮胎
     define('OILPRICE', 10);//油
     define('SPARKPRICE', 4);//火花塞

     //获取总订单数量并打印
     $totalqty = $tireqty + $oilqty + $sparkqty;
     echo "<p>Items ordered: ".$totalqty."<br />";

    //检查是否购买配件并打印
    if ($totalqty == 0 ) {
        echo 'You did not order anything on the previous page!-您没有在上一页订购任何东西！<br />';
    } else {

        if ($tireqty > 0 ) {
            echo htmlspecialchars($tireqty).'tires-轮胎<br />';//轮胎
        }
        
        if ($oilqty > 0 ) {
            echo htmlspecialchars($oilqty).'bottles of oil-几瓶润滑油<br />';//几瓶油
        }
            
        if ($sparkqty > 0 ) {
            echo htmlspecialchars($sparkqty).'spark plugs-火花塞<br />';//火花塞
        }
            
    }

    //计算总价格并打印
    $totalamount = $tireqty * TIREPRICE
                 + $oilqty * OILPRICE
                 + $sparkqty * SPARKPRICE;
    echo "Subtotal-小计: $".number_format($totalamount,2)."<br />";//number_format函数用于格式化数字，取小数点后两位
             
    //计算含税价格并打印
    $taxrate = 0.10;//10%的税率
    $totalamount = $totalamount * (1+$taxrate);
    echo "Total including tax-含税合计:$".number_format($totalamount,2)."</p>";

    //打印购物清单
    echo $outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t"
                       .$sparkqty." spark plugs\t\$".$totalamount
                       ."\t". $address."\n";

    //打开文件进行追加
    try
    {
        //无法打开文件
        if(!(@$fp = fopen("$document_root/orders/orders.txt",'ab'))) {
            throw new fileOpenException();
        }

        //无法锁定该文件
        if (!flock($fp, LOCK_EX)) {
            throw new fileLockException();
        }

        //无法写入文件
        if (!fwrite($fp, $outputstring, strlen($outputstring))) {
            throw new fileWriteException();
        }

        //关闭锁
        flock($fp, LOCK_UN);
        fclose($fp);
        echo "<p>Order written.</p>";
        
    }

    catch ( fileOpenException $foe)
    {
        echo "<p><strong>Orders file could not be opened.<br />
              Please contact our webmaster for help.</strong></p>";
    }

    catch (Exception $e)
    {
        echo "<p><strong>Your order could not be processed at this time.<br />
              Please try again later.</strong></p>";
    }
    //写到这里了

















    echo htmlspecialchars($tireqty).'tires-轮胎<br />';//轮胎
    echo htmlspecialchars($oilqty).'bottles of oil-几瓶油<br />';//几瓶油
    echo htmlspecialchars($sparkqty).'spark plugs-火花塞<br />';//火花塞
    echo '收件地址：'.htmlspecialchars($address);
    
    

    echo "<p>Items ordered-订购的项目:".$totalqty."<br />";
    //echo "<p>测试值，此时:".$totalamount."<br />";
    
    //判断是否购物
        if($totalqty == 0){
        echo "You did not orde anything on the previous page! - 您没有在上一页订购任何内容！ <br />";
        exit;
    }



    echo "<br />轮胎价格：".TIREPRICE,"<br />油价格：".OILPRICE,"<br />火花塞价格：".SPARKPRICE."<br />";


             
             
             
             
             


    

    

    echo "<h1>IF else判断购买轮胎的折扣</h1>";
    if ($tireqty < 10) {
        $discount = 0;
    } elseif (($tireqty >= 10) && ($tireqty <=49)) {
        $discount = 5;
    } elseif (($tireqty >= 50) && ($tireqty <=99)) {
        $discount = 10;
    } elseif ($tireqty >= 100)  {
        $discount = 10;
    }
    
    echo '您享受的折扣是：'.$discount.'<br />';
    
    
    echo "<h1>switch判断渠道来源</h1>";
    
    $find = $_POST['find'];//轮胎
    
    switch($find) {
        case "a" :
            echo "<p>Regular customer - 老客户</p>";
        break;
        
        case "b" :
            echo "<p>Customer referrer by TV advert - 通过电视广告推荐客户</p>";
        break;
        
        case "c" :
            echo "<p>Cusromer referrer by phone directory - 按电话簿列出的客户推荐人</p>";
        break;
        
        case "d" :
            echo "Cusromer referrer by word of mouth - 通过口碑推荐客户";
        break;
        
        default :
            echo "<p>We do not know how this customer found us - 我不知道这位客户是如何找到我们的</p>";
            break;
        
    }
    

    
    
    echo "<h1>文件的写入</h1>";
    //$document_root = $_SERVER['DOCUMENT_ROOT'];//将长名称的变量的内容复制给短名称变量
    
    $outputstring = $date."\t".$tireqty."tires-轮胎\t".$oilqty."oil-油\t".$sparkqty."spark plugs-火花塞\t\$".$totalamount."\t".$address."\n";//写入文件
   
    
    flock($fp,LOCK_EX);//独占锁定（写入的程序）。防止其他进程访问该文件。
    
    if(!$fp) {
        echo "<p><strong>Your order could not be processed at this time.-此时无法处理您的订单
        please try again later.-请稍后再试</strong></p>";
        exit;
    }
    //@$fp = fopen("$document_root/orders/orders.txt",'ab')
    fwrite($fp,$outputstring,strlen($outputstring));
    flock($fp,LOCK_UN);
    fclose($fp);
    echo "<p>Order written.-订单已写入。</p>";
   
    
    
    
    ?>
    


</body>
</html>